<template>
  <div>
    <div>
      <el-input @keydown.enter.native="selectAdmin" v-model="searchName" style="width: 300px; margin: 8px 8px"
                prefix-icon="el-icon-search" placeholder="查询管理员..."></el-input>
      <el-button size="small" type="primary" icon="el-icon-circle-plus-outline" @click="selectAdmin">搜索</el-button>
    </div>
    <div class="admin-container">
      <el-card class="admin-card" v-for="(admin,index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
          <el-button style="float: right; padding: 3px 0 ; color: red" type="text" icon="el-icon-delete"
                     @click="delAdmin(admin)"></el-button>
        </div>
        <div>
          <img :src="admin.userFace" :alt="admin.name" :title="admin.name" class="userFace-img">
        </div>
        <div class="userInfo">
          <div>
            <div>用户名： {{ admin.username }}</div>
            <div>电话： {{ admin.phone }}</div>
            <div>座机： {{ admin.telephone }}</div>
            <div>地址： {{ admin.address }}</div>
            <div>状态：
              <el-popover
                  placement="top-start"
                  title="状态"
                  width="200"
                  trigger="hover"
                  style="margin-right: 4px"
                  content="该用户的状态为禁止时，将不能进行登录等基本功能，请谨慎操作！">
                <el-button slot="reference" type="text" icon="el-icon-s-opportunity"></el-button>
              </el-popover>
              <el-switch
                  v-model="admin.enabled"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="禁用"
                  @change="changeState(admin)">
              </el-switch>
            </div>
          </div>
          <div>
            用户角色:
            <el-tag v-for="(role, index) in admin.roles" :key="index" style="margin: 2px">{{ role.nameZh }}</el-tag>
            <el-popover
                placement="right"
                title="选择角色"
                width="200"
                trigger="click"
                @show="showPop(admin)"
                @hide="hidePop(admin)"
            >
              <el-select v-model="selectedRoles" multiple placeholder="请选择">
                <el-option
                    v-for="(r,index) in allRoles"
                    :key="index"
                    :label="r.nameZh"
                    :value="r.id"
                >
                </el-option>
              </el-select>
              <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
            </el-popover>

          </div>
          <div style="display: flex">
            备注： {{ admin.remark }}
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "SysAdmin",
  data() {
    return {
      admins: [],
      searchName: '',
      allRoles: [],
      selectedRoles: [],
    }
  },
  methods: {

    hidePop(admin) {
      if (this.selectedRoles.length == 0){
        this.$message.error("用户不能没有角色，您可以直接删除该用户！");
        return ;
      }
      let flag = false;
      let roles = [];
      Object.assign(roles, admin.roles);//复制给roles

      if (roles.length != this.selectedRoles.length) {
        flag = true;
      } else {
        for (let i = 0; i < roles.length; i++) {
          let role = roles[i];
          for (let j = 0; j < this.selectedRoles.length; j++) {
            if (role.id == this.selectedRoles[j]) {
              roles.splice(i, 1);
              i--;
              break;
            }
          }
        }
        if (roles.length != 0) {
          flag = true;
        }
      }
      if (flag) {
        let url = "/api/system/admin/role?adminId=" + admin.id;
        this.selectedRoles.forEach(selected => {
          url = url + "&rids=" + selected;
        })
        this.postRequest(url).then(resp => {
          if (resp) {
            this.initAdmins();
          }
        })
      }
    },
    showPop(admin) {
      this.initSelectRoles();
      let roles = admin.roles;
      this.selectedRoles = [];
      roles.forEach(r => {
        this.selectedRoles.push(r.id);
      })
    },
    initSelectRoles() {
      this.getRequest('/api/system/role/').then(resp => {
        if (resp) {
          this.allRoles = resp;
        }
      })
    },
    changeState(admin) {
      this.putRequest('/api/system/admin/', admin).then(resp => {
        if (resp) {
          this.initAdmins();
        }
      })
    },
    delAdmin(admin) {
      this.$confirm('此操作将删除用户： [ ' + admin.name + ' ]的全部数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        this.deleteRequest('/api/system/admin/' + admin.id).then(resp => {
          if (resp) {
            this.initAdmins();
          }
        })

      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    selectAdmin() {
      this.initAdmins()
    },
    initAdmins() {
      this.getRequest('/api/system/admin/' + this.searchName).then(resp => {
        if (resp) {
          this.admins = resp;
        }
      })
    }
  },
  mounted() {
    this.initAdmins();
  }
}
</script>

<style scoped>
.admin-container {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

.admin-card {
  width: 350px;
  margin: 10px;
}

.userFace-img {
  width: 75px;
  height: 75px;
  border-radius: 75px;
}

.userInfo {
  font-size: 15px;
  color: #409eff;
  margin: 4px;

}

</style>